package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.CalendarSignInRes;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.ICalendarSignInMvpView;
import sz.szsmk.citizencard.mvp.presenter.CalendarSignInPresenter;
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity;
import sz.szsmk.citizencard.util.Px2Dp;

/**
 * author : hyt
 * date : 2019/8/19
 * description :
 */
public class SignInCalendarActivity  extends AppBaseActivity implements ICalendarSignInMvpView {
    @Inject
    CalendarSignInPresenter presenter;

    CalendarView mCalendarView;

    TextView tv_todayScore;

    TextView tv_cancleTitle;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.activity_signincalendar);

        mCalendarView = findViewById(R.id.calendarView);

        tv_todayScore = findViewById(R.id.tv_todayScore);

        tv_cancleTitle = findViewById(R.id.tv_cancleTitle);


        init();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        presenter.getCalendarSignIn();
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{ presenter };
    }

    @Override
    protected CharSequence getTopTitle() {
        return "签到领积分";
    }



    private void init(){
        mCalendarView.setEnabled(false);
        mCalendarView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    private Calendar getSchemeCalendar(int year, int month, int day, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setScheme(text);
        return calendar;
    }



    @Override
    public void getCalendarSignInSuccess(CalendarSignInRes calendarSignInRes) {
        tv_todayScore.setText(calendarSignInRes.getCredit());
        List<CalendarSignInRes.SignCreditInfosBean> signCreditInfos = calendarSignInRes.getSignCreditInfos();

        Map<String, Calendar> map = new HashMap<>();
        for(int i=0;i<signCreditInfos.size();i++){
            CalendarSignInRes.SignCreditInfosBean bean = signCreditInfos.get(i);
            //         * credit : 7
            //         * configCredit : 1
            //这里  最后参数  标记使用的  Scheme  传入  0 和 credit   用#分割    0是当天之前 包括当天
            //Scheme    0 是 已经签到的日期（天）   1 是 到未来的一天签到 有多的积分   3 当天日期
            //根据业务逻辑  和  数据可以看出  返回签到的 第0条数据 就是当天  为了可扩展 和其他情况   不使用第0条

            //如果年月日相同
            if(calendarSignInRes.getNowyyyy()==bean.getyyyy()&&
                    calendarSignInRes.getNowMM() == bean.getMM() &&
                    calendarSignInRes.getNowdd()== bean.getdd()
            ){
                //设置 服务器返回的  当前时间（暂时不考虑 国外时区问题）
                String schemeNow = 3+"#"+calendarSignInRes.getNowDate();
                map.put(getSchemeCalendar(calendarSignInRes.getNowyyyy(), calendarSignInRes.getNowMM(), calendarSignInRes.getNowdd(), schemeNow).toString(),
                        getSchemeCalendar(calendarSignInRes.getNowyyyy(), calendarSignInRes.getNowMM(), calendarSignInRes.getNowdd(), schemeNow));

                changeMonth(calendarSignInRes.getNowMM());
            }else{
                String scheme = 0+"#"+bean.getCredit();
                map.put(getSchemeCalendar(bean.getyyyy(), bean.getMM(), bean.getdd(), scheme).toString(),
                        getSchemeCalendar(bean.getyyyy(), bean.getMM(), bean.getdd(), scheme));
            }
        }
        CalendarSignInRes.RewardDateBean rewardDate = calendarSignInRes.getRewardDate();
        String scheme = 1+"#"+rewardDate.getCreditX();
        map.put(getSchemeCalendar(rewardDate.getyyyy(), rewardDate.getMM(), rewardDate.getdd(), scheme).toString(),
                getSchemeCalendar(rewardDate.getyyyy(), rewardDate.getMM(), rewardDate.getdd(), scheme));


        mCalendarView.setSchemeDate(map);
    }

    private void changeMonth(int nowMM) {
        switch (nowMM){
            case 1:
                tv_cancleTitle.setText("签到日历（一月）");
                break;
            case 2:
                tv_cancleTitle.setText("签到日历（二月）");
                break;
            case 3:
                tv_cancleTitle.setText("签到日历（三月）");
                break;
            case 4:
                tv_cancleTitle.setText("签到日历（四月）");
                break;
            case 5:
                tv_cancleTitle.setText("签到日历（五月）");
                break;
            case 6:
                tv_cancleTitle.setText("签到日历（六月）");
                break;
            case 7:
                tv_cancleTitle.setText("签到日历（七月）");
                break;
            case 8:
                tv_cancleTitle.setText("签到日历（八月）");
                break;
            case 9:
                tv_cancleTitle.setText("签到日历（九月）");
                break;
            case 10:
                tv_cancleTitle.setText("签到日历（十月）");
                break;
            case 11:
                tv_cancleTitle.setText("签到日历（十一月）");
                break;
            case 12:
                tv_cancleTitle.setText("签到日历（十二月）");
                break;
        }

    }
}